package com.msos.servlet.reChargeServlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.msos.model.ReCharge.ReCharge;
import com.msos.model.respBean.RespBean;
import com.msos.service.reChargeService.reChargeService;
import com.msos.utils.CommonsUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/reCharge")
public class reChargeServlet extends HttpServlet {
    reChargeService rechargeService=new reChargeService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer r1;
        String cardNumber=request.getParameter("cardNumber");
        String password=request.getParameter("password");
        String endDate=request.getParameter("endDate");
        r1=rechargeService.addReCharCard(cardNumber,password,endDate);
        RespBean respBean=null;
        if(r1== CommonsUtils.SUCCESS){
            respBean= RespBean.ok("添加充值卡成功");
        }else if (r1==CommonsUtils.EXIST){
            respBean=RespBean.error("充值卡已存在");
        }else if(r1==CommonsUtils.FAIL){
            respBean=RespBean.error("添加充值卡成功");
        }else {
            respBean=RespBean.error("发生异常");
        }
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper om=new ObjectMapper();
        PrintWriter out = response.getWriter();
        String json= om.writeValueAsString(respBean);
        out.write(json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if("add".equals(action)){
        request.getRequestDispatcher("WEB-INF/jsp/reCharge/addReCharge.jsp").forward(request,response);
        }else if("all".equals(action)){
            request.getRequestDispatcher("WEB-INF/jsp/reCharge/allReCharge.jsp").forward(request,response);
        } else if("data".equals(action)){
            List<ReCharge> list=null;
            list=rechargeService.getAllCard();
            response.setContentType("application/json;charset=utf-8");
            ObjectMapper om=new ObjectMapper();
            PrintWriter out = response.getWriter();
            String json= om.writeValueAsString(list);
            out.write(json);


        }
    }
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String[] ridList= request.getParameterValues("rid");
        Integer r1;
        if(ridList.length>0){
            r1= rechargeService.recharCardsDelete(ridList);
        }else {
            r1=CommonsUtils.NOVALUES;
        }
        RespBean respBean=null;
        if(r1== CommonsUtils.SUCCESS){
            respBean= RespBean.ok("删除多充值卡成功");
        }else if (r1==CommonsUtils.NOVALUES){
            respBean=RespBean.error("ridList为空");
        }else if(r1==CommonsUtils.FAIL){
            respBean=RespBean.error("删除多充值卡失败");
        }else {
            respBean=RespBean.error("发生异常");
        }
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper om=new ObjectMapper();
        PrintWriter out = response.getWriter();
        String json= om.writeValueAsString(respBean);
        out.write(json);
    }
}
